// pages/address/address.js
Page({

  data: {
    charge:'none',
    addressarray:[
    ],
    length:"",
    name:'',
    number:'',
    address:'',
    which:'',
    fromwhere:'',
    case:'',
 

  },

  onLoad(options){
    // console.log(options)
    if(options.fromwhere){
      this.setData({
        fromwhere:1
      })
    }
  },

  onShow(){
    var that = this;
    var openid = wx.getStorageSync('openid');
    wx.request({
      url: 'http://127.0.0.1:8080/address/selectAddress',
      method:"POST",
      data:{openid:openid},
      success(res){
        if(res.data.length != 0){
          that.setData({
            addressarray:res.data,
            length:res.data.length
          })
        }
        // console.log(res)
      }
    })
  },
// 弹出修改信息框
  changeinfo(e){
    var index = e.currentTarget.dataset.index;
    var value = this.data.addressarray[index];
    var data = this.data;

    this.setData({
      charge:'true',
      name:value.rentName,
      number:value.rentPhone,
      address:value.rentAddress,
      which:index,
      case:1
    })

   
  },
  // 取消修改界面
  cancel(){
    this.setData({
      charge:'none',
      which:''
    })
  },
  // 确认修改
  getname(e){
    this.setData({
      name:e.detail.value
    })
  },
  getnumber(e){
    this.setData({
      number:e.detail.value
    })
  },
  getaddress(e){
    this.setData({
      address:e.detail.value
    })
  },
  sure(e){
    var that = this;
    var data  = this.data;
    console.log(data.case)
    switch(this.data.case){
      case 0 :
        if(data.name != "" && data.number != "" && data.address != ""){
          console.log('sadasdas')
          if(data.length >= 0){
            var flag = 1;
            console.log(data.length)
            for(var i=0;i<data.length;i++){
              if(data.addressarray[i].rentName == data.name && data.addressarray[i].rentPhone == data.number && data.addressarray[i].rentAddress == data.address){
                flag = 0;
              }
            }
            if(flag == 1){
              console.log(that.data)
              var openid = wx.getStorageSync('openid');
              var data = {
                openid:openid,
                name:data.name,
                phone:data.number,
                address:data.address
              }
              wx.request({
                url: 'http://127.0.0.1:8080/address/addAddress',
                method:"POST",
                data:data,
                success(res){
                  console.log(res)
                  if(res.data){
                    that.setData({
                      charge:"none"
                    })
                    that.onShow();
                  }
                }
              })
            }else{
              wx.showToast({
                title: '已存在该地址',
                duration: 2000,
                icon:"none",
                success: (res) => {},
              })
            }
          }
        }
        break;
      
      case 1:{
       
        var value = this.data.addressarray[that.data.which];
        if(data.name != "" && data.phone != "" && data.address != ""){
          if(data.name != value.rentName || data.number != value.rentPhone || data.address != value.rentAddress){
            data = {
              aftername:data.name,
              afterphone:data.number,
              afteraddress:data.address,
        
              openid:wx.getStorageSync('openid'),
              name:value.rentName,
              number:value.rentPhone,
              address:value.rentAddress,
            };
        
            wx.request({
              url: 'http://127.0.0.1:8080/address/updateAddress',
              method:"POST",
              data:data,
              success(res){
                // console.log(res);
                if(res.data){
                  that.setData({
                    charge:"none",
                    which:""
                  })
                  that.onShow();
                }
              }
            })
          }else{
            wx.showToast({
              title: '地址已存在',
              duration: 2000,
              icon: "none",
              success: (res) => {},
            })
          }
  
        }
        break;
      };
  
    }
    
  },
  //删除地址
  del(e){
    var that = this;
    wx.showModal({
      title: '确定删除该地址吗？',
      success: (result) => {
        // console.log(result)
        var index = e.currentTarget.dataset.index;
        // console.log(index)
          if(result.confirm){
            var data1 = that.data.addressarray[index];
            // console.log(data1)
            var data = {
              openid:data1.userID,
              name:data1.rentName,
              phone:data1.rentPhone,
              address:data1.rentAddress,
            }
            wx.request({
              url: 'http://127.0.0.1:8080/address/delAddress',
              method:"POST",
              data:data,
              success(res){
                // console.log(res)
                if(res.data){
                  that.onShow()
                }
              }
            })

          }
      },
    })
  },
  // 添加地址
  createnew(){
    // console.log(this.data.which)
    this.setData({
      charge:true,
      name:'',
      number:'',
      address:'',
      case:0
    })
    console.log(this.data.charge)
  },

  //在购买页面选择地址
  chooseaddress(e){
    if(this.data.fromwhere == 1){ 
      var index = e.currentTarget.dataset.index;
      let array = this.data.addressarray[index];
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.setData({
       name:array.rentName,
       phone:array.rentPhone,
       address:array.rentAddress
      })
      // 关闭当前页面，返回上一页面
      wx.navigateBack({
        delta: 1
      })

    }
  }

})